<template>
  <div>
    <div class="slot">
      <div class="top">
        <div class="return" @click="goback">
          <img src="@/assets/my/zuo.png" alt />
        </div>
        <span>下级收益</span>
      </div>
    </div>
    <div class="main">
      <div class="main_top">
        <div class="top_title">
          <div class="title_left">可提现佣金</div>
          <div class="title_right" @click="yongjinfo">佣金明细 ></div>
        </div>
        <div class="price">
          <div class="price_left">
            ￥
            <span>{{allprice}}</span>
          </div>
          <div class="tx_btn" @click="yongjtx">提现</div>
        </div>
      </div>
      <div class="main_center">
        <div class="top_title">
          <div :class="active==1?'li1':'li'" @click="changeactive(1)">
            <div class="p1">一级下级</div>
            <span class="span"></span>
          </div>
          <div :class="active==2?'li1':'li'" @click="changeactive(2)">
            <div class="p1">二级下级</div>
            <span class="span"></span>
          </div>
        </div>
        <div class="center">
          <div class="main1">
            <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
              <van-list
                :finished="finished"
                @load="onLoad"
                v-model="loading"
                finished-text="暂无数据了~"
                :immediate-check="false"
              >
                <div class="li2" v-for="item in list" :key="item.name">
                  <div class="left">
                    <img :src="item.avatar" alt />
                    <div class="info">
                      <span class="top1">{{item.name}}</span>
                      <span class="time"></span>
                    </div>
                  </div>
                  <div class="right">
                    ￥
                    <span v-if="active==1">{{item.up1_amount}}</span>
                    <span v-if="active==2">{{item.up2_amount}}</span>
                  </div>
                </div>
              </van-list>
            </van-pull-refresh>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      allprice:0,
      active: 1, //请求状态
      list: [],
      loading: false, //是否在上拉加载
      isLoading: false, //是否在刷新
      finished: false, //是否加载完成
      page: 1,
      total: 0,
      raw: 20,
    };
  },
  mounted() {
    this.token = window.localStorage.getItem("token")
    this.onRefresh();
  },
  methods: {
    goback() {
      this.$router.back();
    },
    yongjinfo() {
      this.$router.push("/yongjinfo");
    },
    changeactive(active) {
      this.active = active;
      this.onRefresh();
    },
    // 下拉刷新
    onRefresh() {
      this.page = 1;
      this.finished = false;
      this.getList();
    },
    //上拉加载
    onLoad() {
      // 异步更新数据
      console.log("加载");
      this.page++;
      axios
        .post("/index/user/lower_profit", this.$qs.stringify({
          token: this.token,
          level: this.active,
          page: this.page,
        }))
        .then((res) => {
          var list1 = res.data.data.data;
          this.total = res.data.data.total;
          list1.forEach((item) => {
            this.list.push(item);
          });
          // 加载状态结束
          this.loading = false;
          // 数据全部加载完成
          if (this.list.length >= this.total) {
            this.finished = true;
          }
        });
    },
    getList() {
      axios
        .post("/index/user/lower_profit", this.$qs.stringify({
          token: this.token,
          level: this.active,
          page: this.page,
        }))
        .then((res) => {
          this.list = res.data.data.data;
          this.allprice = res.data.data.fee_balance
          this.total = res.data.data.total;
          this.isLoading = false;
          if (this.list.length >= this.total) {
            this.finished = true;
          }
        });
    },
    yongjtx() {
      axios
        .post("/index/user/cash_out", this.$qs.stringify({
          token: this.token,
          state: 2,
        }))
        .then((res) => {
          if (res.data.code === 1) {
            this.$toast(res.data.msg);
            this.allprice = "0.00"
          } else {
            this.$toast(res.data.msg);
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.slot {
  width: 100%;
  height: 44px;
  .top {
    width: 100%;
    height: 44px;
    display: flex;display: -webkit-flex;
    justify-content: center;
    align-items: center;
    background: white;
    position: fixed;
    top: 0;
    left: 0;
    .return {
      width: 38px;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;display: -webkit-flex;
      justify-content: center;
      align-items: center;
      img {
        width: 8px;
        height: 15px;
      }
    }
    span {
      font-size: 18px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #000000;
      line-height: 44px;
    }
  }
}
.main {
  width: 100%;
  margin-top: 10px;
  .main_top {
    width: 345px;
    height: 100px;
    background: linear-gradient(90deg, #ffe200 0%, #ffc700 100%);
    box-shadow: 1px 15px 30px 0px rgba(255, 200, 0, 0.4);
    border-radius: 10px 10px 0px 0px;
    margin: auto;
    .top_title {
      margin-left: 15px;
      padding-top: 20px;
      display: flex;display: -webkit-flex;
      align-items: center;
      .title_left {
        font-size: 13px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #222222;
        line-height: 13px;
        margin-right: 7px;
      }
      .title_right {
        width: 70px;
        height: 20px;
        background: #ffffff;
        opacity: 0.6;
        border-radius: 5px;
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #444444;
        line-height: 20px;
        text-align: center;
      }
    }
    .price {
      display: flex;display: -webkit-flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 15px;
      .price_left {
        margin-left: 15px;
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #000000;
        line-height: 12px;
        span {
          font-size: 23px;
          font-weight: bold;
          font-family: PingFang-SC-Bold;
        }
      }
      .tx_btn {
        width: 65px;
        height: 30px;
        background: #ffffff;
        border-radius: 15px;
        margin-right: 15px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #000000;
        line-height: 30px;
        text-align: center;
      }
    }
  }
  .main_center {
    width: 100%;
    background: white;
    .top_title {
      width: 100%;
      height: 45px;
      background: #ffffff;
      display: flex;display: -webkit-flex;
      border-bottom: 1px solid #f8f8f8;
      .li {
        width: 50%;
        height: 100%;
        display: flex;display: -webkit-flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        text-align: center;
        .p1 {
          font-size: 15px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #888888;
          line-height: 15px;
        }
      }
      .li1 {
        width: 50%;
        height: 100%;
        display: flex;display: -webkit-flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        text-align: center;
        position: relative;
        .p1 {
          font-size: 18px;
          font-family: PingFang SC;
          font-weight: bold;
          color: #000000;
          line-height: 18px;
        }
        .span {
          width: 28px;
          height: 4px;
          background: #fdd40a;
          border-radius: 2px;
          position: absolute;
          bottom: 0;
        }
      }
    }
    .center {
      width: 100%;
      background: white;
      .main1 {
        width: 345px;
        margin: auto;
        .li2 {
          width: 100%;
          height: 75px;
          border-bottom: 1px solid #f8f8f8;
          display: flex;display: -webkit-flex;
          align-items: center;
          justify-content: space-between;
          .left {
            display: flex;display: -webkit-flex;
            align-items: center;
            img {
              width: 35px;
              height: 35px;
              margin-right: 10px;
              border-radius: 50%;
            }
            .info {
              display: flex;display: -webkit-flex;
              flex-direction: column;
              align-items: flex-start;
              .top1 {
                font-size: 16px;
                font-family: PingFang SC;
                font-weight: 500;
                color: #222222;
                line-height: 16px;
              }
              .time {
                font-size: 12px;
                font-family: PingFang SC;
                font-weight: 500;
                color: #bcbcbc;
                line-height: 12px;
                margin-top: 10px;
              }
            }
          }
          .right {
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #ff521f;
            line-height: 12px;
            span {
              font-size: 17px;
              font-weight: bold;
            }
          }
        }
      }
    }
  }
}
</style>